<?
/**
* Traduction de la date de l'anglais vers le français
*
* @param date la date en anglais de type string
* @return la date avec les éléments en anglais traduits
*/
function traduire_dates_en_to_fr($date)
	{
	$date=ereg_replace ("Mon", "Lun", $date);
	$date=ereg_replace ("Tue", "Mar", $date);
	$date=ereg_replace ("Wed", "Mer", $date);
	$date=ereg_replace ("Thu", "Jeu", $date);
	$date=ereg_replace ("Fri", "Ven", $date);
	$date=ereg_replace ("Sat", "Sam", $date);
	$date=ereg_replace ("Sun", "Dim", $date);
	
	$date=ereg_replace ("Lunday", "Lundi", $date);
	$date=ereg_replace ("Marsday", "Mardi", $date);
	$date=ereg_replace ("Mernesday", "Mercredi", $date);
	$date=ereg_replace ("Jeursday", "Jeudi", $date);
	$date=ereg_replace ("Venday", "Vendredi", $date);
	$date=ereg_replace ("Samurday", "Samedi", $date);
	$date=ereg_replace ("Dimday", "Dimanche", $date);
	
	$date=ereg_replace ("Dec", "Déc", $date);
	$date=ereg_replace ("Feb", "Fév", $date);
	$date=ereg_replace ("Apr", "Avr", $date);
	$date=ereg_replace ("May", "Mai", $date);
	$date=ereg_replace ("Aug", "Aoû", $date);
	
	$date=ereg_replace ("January", "Janvier", $date);
	$date=ereg_replace ("February", "Février", $date);
	$date=ereg_replace ("March", "Mars", $date);
	$date=ereg_replace ("April", "Avril", $date);
	$date=ereg_replace ("June", "Juin", $date);
	$date=ereg_replace ("July", "Juillet", $date);
	$date=ereg_replace ("August", "Août", $date);
	$date=ereg_replace ("September", "Septembre", $date);
	$date=ereg_replace ("October", "Octobre", $date);
	$date=ereg_replace ("November", "Novembre", $date);
	$date=ereg_replace ("December", "Décembre", $date);

	return $date;
	}
	
/**
* création d'une liste déroulante
*
* @param jourdepart jour de début de la liste
* @param jourfin jour de fin de la liste
* @param nbjours nb de jour de la lise (le + tard prend le dessus)
* @param selectionne la date pré-sélectionnée
* @param nom le paramètre "name" de la liste
* @param highlite SI les jours d'ouverture doivent être sélectionnés
* @param format_date le format d'affichage de la date dans la liste par défaut "01.12.08 jeu"
* @param other_param autre paramètre de <select ... >
* @return une liste sous forme de <select>
*/
function dates_select_highlite_openups($jourdepart, $jourfin, $nbjours, $selectionne, $nom, $highlite=TRUE, $format_date="d.m.Y D", $other_param=FALSE)
	{
	$depart=strtotime($jourdepart);
	$fin=strtotime($jourfin);
	$nbsecondes=(int)$nbjours*24*60*60;
	
	
	$interval=FALSE;
	if ($depart+$nbsecondes > $fin) $fin=$depart+$nbsecondes;
	if ($highlite)
		{
		$sql_select="SELECT ouverture, ouvert FROM ouverture WHERE ouvert=1 AND ouverture BETWEEN NOW() AND '".date('Y-m-d',$fin)."'";
		$req=mysql_query($sql_select) or die('Erreur SQL !<br>'.$sql_select.mysql_error());
		while($data = mysql_fetch_assoc($req))
			{
			$tab_date[substr($data['ouverture'],0,10)]=$data['ouvert'];
			}
		}
	
	//affichage de la liste
	$return = '<select name="'.$nom.'" '.$other_param.'>';
	for ($i = $depart; $i <= $fin; $i+=24*60*60)
		{
		$dategeneree=traduire_dates_en_to_fr(date($format_date, $i));
		$date_table=date('Y-m-d', $i);
		
		if ($highlite)
			{
			if(array_key_exists($date_table,$tab_date)) $style_menu='style="background-color:#9DF99F;"';		
			else $style_menu='style="background-color:#FF0000;"';		
			}
				
		$selected="";
		
		if ($date_table==$selectionne) $selected="selected ";
		$return .= '<option "'.$selected.'" value="'.$date_table.'" '.$style_menu.'>'.$dategeneree.'</option>';
		
		}
	return $return.'</select>';
	}
/**
* recherche de la prochaine date d'ouverture
*
* @param from_date jour de départ de la recherche
* @param format, U = timestamp par défaut voir http://www.php.net/manual/en/function.date.php
* @return la date trouvée 
*/
function next_open($from_date='NOW()', $format='U')
	{
	if ($from_date!='NOW()') $from_date= "'".$from_date."'";
	$sql_next_open="SELECT ouverture FROM ouverture WHERE ouverture >=".$from_date." AND ouvert=1 ORDER BY ouverture LIMIT 0,1";
	$res_next_open=mysql_query($sql_next_open);
	$ligne_next_open=mysql_fetch_array ($res_next_open);
	return date($format,strtotime($ligne_next_open['ouverture']));
	}

/**
* recherche de la précédente date d'ouverture
*
* @param from_date jour de départ de la recherche
* @param format, U = timestamp par défaut voir http://www.php.net/manual/en/function.date.php
* @return la date trouvée 
*/
function previous_open($from_date='NOW()', $format='U')
	{
	if ($from_date!='NOW()') $from_date= "'".$from_date."'";
	$sql_next_open="SELECT ouverture FROM ouverture WHERE ouverture <=".$from_date." AND ouvert=1 ORDER BY ouverture DESC LIMIT 0,1";
	$res_next_open=mysql_query($sql_next_open);
	$ligne_next_open=mysql_fetch_array ($res_next_open);
	return date($format,strtotime($ligne_next_open['ouverture']));
	}
	
//////////////////////////////////////////////////////////////////////////////////////////////////
// jusque là, c'est clean!!!
//////////////////////////////////////////////////////////////////////////////////////////////////

/**
* création de trois champ "select" de date format PHP en
*
* @param from_date jour de départ de la recherche
* @param format, U = timestamp par défaut voir http://www.php.net/manual/en/function.date.php
* @return la date trouvée 
*/
function dates_select_trois($select_list, $annee_debut, $annee_fin, $nom, $other_param=FALSE)
	{
	$selectedTime=strtotime($select_list);
	$annee=date('Y',$selectedTime);
	$mois=date('m',$selectedTime);
	$jour=date('d',$selectedTime);
	
	//affichage de la liste
	print '<select name="'.$nom.'_d" '.$other_param.'>';
	for ($i = 1; $i <= 31; $i++)
		{
		$ia=$i;
		if ($i<10) $ia="0".$ia;
		if ($ia==$jour) $selected_d='selected ';
		else $selected_d="";
		print '<option '.$selected_d.' value="'.$ia.'">'.$i.'</option>';
		}
	print '</select>';
	print '<select name="'.$nom.'_m">';
	for ($i = 1; $i <= 12; $i++)
		{
		$ia=$i;
		if ($i<10) $ia="0".$ia;
		if ($ia==$mois) {$selected_m='selected ';}
		else $selected_m="";
		print "<option ".$selected_m.' value="'.$ia.'">'.$i.'</option>';
		}
	print '</select>';
	print '<select name="'.$nom.'_Y">';
	for ($i = $annee_debut; $i <= $annee_fin; $i++)
		{
		if ($i==$annee) $selected_Y='selected ';
		else $selected_Y="";
		print '<option '.$selected_Y.' value="'.$i.'">'.$i.'</option>';
		}
	print '</select>';
	}

// affiche une liste select d'heures
function heures_select($heuredepart, $heurefin, $interval, $duree, $nom,  $heure_select, $format_heure)
{
// calcule l'heure de d�part en seconde depuis le 1.1.2000
$H_depart=substr($heuredepart, -8, 2);
$i_depart=substr($heuredepart, -5, 2);
$s_depart=substr($heuredepart, -2, 2);
$time_depart=mktime($H_depart,$i_depart,$s_depart,"1","1","2000");
// calcule l'heure de fin en seconde depuis le 1.1.2000
$H_fin=substr($heurefin, -8, 2);
$i_fin=substr($heurefin, -5, 2);
$s_fin=substr($heurefin, -2, 2);
$time_fin=mktime($H_fin,$i_fin,$s_fin,"1","1","2000");
// calcule l'interval en secondes
$H_interval=substr($interval, -8, 2);
$i_interval=substr($interval, -5, 2);
$s_interval=substr($interval, -2, 2);
$time_interval=3600*$H_interval+60*$i_interval+$s_interval;
// calcule la dur�e en secondes
$H_duree=substr($duree, -8, 2);
$i_duree=substr($duree, -5, 2);
$s_duree=substr($duree, -2, 2);
$time_duree=3600*$H_duree+60*$i_duree+$s_duree;
// calcule l'heure s�lectionn�e
$H_select=substr($heure_select, -8, 2);
$i_select=substr($heure_select, -5, 2);
$s_select=substr($heure_select, -2, 2);
$time_select=mktime($H_select,$i_select,$s_select,"1","1","2000");

print "<select name=\"$nom\">";
if ($interval<0) {print "<option selected value=\"0\">ERREUR</option>";}

for ($i = $time_depart; $i <= $time_fin; $i=$i+$time_interval)
{
$heuregeneree=date($format_heure, $i);
$select_genere=date($format_heure, $time_select);
$heure_table=date('H:i:s', $i);
$select_table=date('H:i:s', $time_select);
// s�lection de l'heure
if ($i==$time_select) {$selected="selected ";}

// si l'heure s�lectionn�e n'est pas dans la liste
elseif ($i > $time_select-$time_interval && $i < $time_select+$time_interval && $i > $time_select) {echo "<option selected value=\"$select_table\">".$select_genere."</option>";}
else {$selected="";}
echo "<option ".$selected."value=\"$heure_table\">".$heuregeneree."</option>";
}

print "</select>";
}

// affiche une liste de 2 select d'heures + minutes
function heures_2_select($interval, $nom,  $heure_select)
{

// Heure s�lectionn�e
$H_select=substr($heure_select, -8, 2);
$i_select=substr($heure_select, -5, 2);

print "<select name=\"".$nom."_H\">";
for ($i = 00; $i <= 23; $i++)
{

if ($i==$H_select) {$selected="selected ";}
else {$selected="";}

echo "<option ".$selected."value=\""; if ($i<10) {print "0";} echo"$i\">".$i."</option>";
}
print "</select>";
print "<select name=\"".$nom."_i\">";
for ($i = 00; $i <= 59; $i=$i+$interval)
{
// s�lection de la minute
if ($i==$i_select) {$selected="selected ";}
// si la minute n'est pas comprise sur l'interval.
elseif ($i > $i_select-$interval && $i < $i_select+$interval && $i > $i_select) {echo "<option selected value=\"$i_select\">".$i_select."</option>";}
else {$selected="";}

echo "<option ".$selected."value=\""; if ($i<10) {print "0";} echo"$i\">".$i."</option>";
}

print "</select>";
}


// affichage d'une liste num�rique de x � y
function liste_courte($nb_depart, $nb_fin, $nom, $selected)
{
print "<select name=\"$nom\">";
for ($i = $nb_depart; $i <= $nb_fin; $i++)
	{
	if ($selected==$i) {$selected_print="SELECTED";}
	else {$selected_print="";}
	echo "<option value=\"$i\" ".$selected_print." >".$i."</option>";
	}
print "</select>";
}

//cr�ation d'un lien vers une fen�tre sans menu de dimentions x,y
function mklink_simple($url, $x, $y, $top, $left, $name)
{
return "<a href=\"\" onClick='enterWindow=window.open(\"".$url."\",\"".$name."\",\"width=".$x.",height=".$y.",top=".$top.",left=".$left."\"); return false'>";
}

//liste de pays
function liste_pays_simple($nom)
{
print "
<SELECT name=\"$nom\">
<option value=\"AF\">Afghanistan</option>
<option value=\"ZA\">Afrique du Sud</option>
<option value=\"AL\">Albanie</option>
<option value=\"DZ\">Algérie</option>
<option value=\"DE\">Allemagne</option>
<option value=\"AD\">Andorre</option>
<option value=\"AO\">Angola</option>
<option value=\"AG\">Antigua &amp; Barbuda</option>
<option value=\"AN\">Antilles Néerlandaises</option>
<option value=\"SA\">Arabie Saoudite</option>
<option value=\"AR\">Argentine</option>
<option value=\"AM\">Arménie</option>
<option value=\"AU\">Australie</option>
<option value=\"AT\">Autriche</option>
<option value=\"AJ\">Azerbaïdjan</option>
<option value=\"BS\">Bahamas</option>
<option value=\"BH\">Bahreïn</option>
<option value=\"BD\">Bangladesh</option>
<option value=\"BB\">Barbades</option>
<option value=\"BE\">Belgique</option>
<option value=\"BZ\">Belize</option>
<option value=\"BJ\">Bénin</option>
<option value=\"BT\">Bhoutan</option>
<option value=\"BY\">Biélorussie</option>
<option value=\"BO\">Bolivie</option>
<option value=\"BA\">Bosnie-Herzégovine</option>
<option value=\"BW\">Botswana</option>
<option value=\"BR\">Brésil</option>
<option value=\"BN\">Brunei</option>
<option value=\"BG\">Bulgarie</option>
<option value=\"BF\">Burkina Faso</option>
<option value=\"BI\">Burundi</option>
<option value=\"KH\">Cambodge</option>
<option value=\"CM\">Cameroun</option>
<option value=\"CA\">Canada</option>
<option value=\"CV\">Cap Vert</option>
<option value=\"CF\">Centre Afrique (Rep.)</option>
<option value=\"IC\">Channel Islands</option>
<option value=\"CL\">Chili</option>
<option value=\"CN\">Chine</option>
<option value=\"CY\">Chypre</option>
<option value=\"CO\">Colombie</option>
<option value=\"KM\">Comores</option>
<option value=\"CG\">Congo</option>
<option value=\"ZR\">Congo (Rep. Dem.)</option>
<option value=\"KP\">Corée du Nord</option>
<option value=\"KR\">Corée du Sud</option>
<option value=\"CR\">Costa Rica</option>
<option value=\"CI\">Côte d'Ivoire</option>
<option value=\"HR\">Croatie</option>
<option value=\"CU\">Cuba</option>
<option value=\"DK\">Danemark</option>
<option value=\"DJ\">Djibouti</option>
<option value=\"DM\">Dominique</option>
<option value=\"EG\">Egypte</option>
<option value=\"AE\">Emirats Arabes Unis</option>
<option value=\"EC\">Equateur</option>
<option value=\"ER\">Eritrée</option>
<option value=\"ES\">Espagne</option>
<option value=\"EE\">Estonie</option>
<option value=\"ET\">Ethiopie</option>
<option value=\"FK\">Falklands</option>
<option value=\"FI\">Finlande</option>
<option value=\"FR\">France</option>
<option value=\"GA\">Gabon</option>
<option value=\"GM\">Gambie</option>
<option value=\"GE\">Géorgie</option>
<option value=\"GH\">Ghana</option>
<option value=\"GI\">Gibraltar</option>
<option value=\"GR\">Grèce</option>
<option value=\"GD\">Grenada</option>
<option value=\"GP\">Guadeloupe</option>
<option value=\"GT\">Guatemala</option>
<option value=\"GN\">Guinée</option>
<option value=\"GQ\">Guinée équatoriale</option>
<option value=\"GW\">Guinée-Bissau</option>
<option value=\"GY\">Guyane</option>
<option value=\"GF\">Guyane française</option>
<option value=\"HT\">Haïti</option>
<option value=\"HN\">Honduras</option>
<option value=\"HK\">Hong Kong</option>
<option value=\"HU\">Hongrie</option>
<option value=\"MI\">Ile de Man</option>
<option value=\"MU\">île Maurice</option>
<option value=\"FO\">Iles Féroé</option>
<option value=\"IN\">Inde</option>
<option value=\"ID\">Indonesie</option>
<option value=\"IQ\">Irak</option>
<option value=\"IR\">Iran</option>
<option value=\"IE\">Irlande</option>
<option value=\"IS\">Islande</option>
<option value=\"IL\">Israël</option>
<option value=\"IT\">Italie</option>
<option value=\"JM\">Jamaïque</option>
<option value=\"JP\">Japon</option>
<option value=\"JO\">Jordanie</option>
<option value=\"KZ\">Kazakhstan</option>
<option value=\"KE\">Kenya</option>
<option value=\"KG\">Kirghizie</option>
<option value=\"KW\">Koweït</option>
<option value=\"LA\">Laos</option>
<option value=\"VA\">Le Vatican</option>
<option value=\"LS\">Lesotho</option>
<option value=\"LV\">Lettonie</option>
<option value=\"LB\">Liban</option>
<option value=\"LR\">Libéria</option>
<option value=\"LY\">Libye</option>
<option value=\"LI\">Liechtenstein</option>
<option value=\"LT\">Lituanie</option>
<option value=\"LU\">Luxembourg</option>
<option value=\"MO\">Macao</option>
<option value=\"MK\">Macédoine</option>
<option value=\"MG\">Madagascar</option>
<option value=\"MY\">Malaisie</option>
<option value=\"MW\">Malawi</option>
<option value=\"ML\">Mali</option>
<option value=\"MT\">Malte</option>
<option value=\"MA\">Maroc</option>
<option value=\"MQ\">Martinique</option>
<option value=\"MR\">Mauritanie</option>
<option value=\"YT\">Mayotte</option>
<option value=\"MX\">Mexique</option>
<option value=\"MD\">Moldavie</option>
<option value=\"MC\">Monaco</option>
<option value=\"MN\">Mongolie</option>
<option value=\"MS\">Montserrat</option>
<option value=\"MZ\">Mozambique</option>
<option value=\"MM\">Myanmar</option>
<option value=\"NA\">Namibie</option>
<option value=\"NP\">Népal</option>
<option value=\"NI\">Nicaragua</option>
<option value=\"NE\">Niger</option>
<option value=\"NG\">Nigeria</option>
<option value=\"NO\">Norvège</option>
<option value=\"NZ\">Nouvelle-Zélande</option>
<option value=\"OM\">Oman</option>
<option value=\"UG\">Ouganda</option>
<option value=\"UZ\">Ouzbékistan</option>
<option value=\"PK\">Pakistan</option>
<option value=\"PA\">Panama</option>
<option value=\"PG\">Papouasie N. Guinée</option>
<option value=\"PY\">Paraguay</option>
<option value=\"NL\">Pays-Bas</option>
<option value=\"PE\">Pérou</option>
<option value=\"PH\">Philippines</option>
<option value=\"PL\">Pologne</option>
<option value=\"PT\">Portugal</option>
<option value=\"PR\">Puerto Rico</option>
<option value=\"QA\">Qatar</option>
<option value=\"DO\">Rép. Dominicaine</option>
<option value=\"CZ\">Rép. Tchèque</option>
<option value=\"RE\">Réunion</option>
<option value=\"RO\">Roumanie</option>
<option value=\"UK\">Royaume-Uni</option>
<option value=\"RU\">Russie</option>
<option value=\"RW\">Rwanda</option>
<option value=\"KN\">Saint Kitts &amp; Nevis</option>
<option value=\"VC\">Saint Vincent</option>
<option value=\"LC\">Sainte-Lucie</option>
<option value=\"SV\">Salvador</option>
<option value=\"SM\">San Marin</option>
<option value=\"ST\">S�o Tom� et Préncipe</option>
<option value=\"SN\">Sénégal</option>
<option value=\"SL\">Sierra Leone</option>
<option value=\"SG\">Singapour</option>
<option value=\"SK\">Slovaquie</option>
<option value=\"SI\">Slovénie</option>
<option value=\"SO\">Somalie</option>
<option value=\"SD\">Soudan</option>
<option value=\"LK\">Sri Lanka</option>
<option value=\"SE\">Suède</option>
<option value=\"CH\" selected=\"1\">Suisse</option>
<option value=\"SR\">Suriname</option>
<option value=\"SZ\">Swaziland</option>
<option value=\"SY\">Syrie</option>
<option value=\"TJ\">Tadjikistan</option>
<option value=\"TW\">Taiwan</option>
<option value=\"TZ\">Tanzanie</option>
<option value=\"TD\">Tchad</option>
<option value=\"TH\">Thaïlande</option>
<option value=\"TG\">Togo</option>
<option value=\"TT\">Trinit�-et-Tobago</option>
<option value=\"TN\">Tunisie</option>
<option value=\"TM\">Turkménistan</option>
<option value=\"TR\">Turquie</option>
<option value=\"UA\">Ukraine</option>
<option value=\"UY\">Uruguay</option>
<option value=\"US\">USA</option>
<option value=\"UE\">Venezuela</option>
<option value=\"VN\">Viét-nam</option>
<option value=\"YE\">Yémen</option>
<option value=\"YU\">Yougoslavie</option>
<option value=\"ZM\">Zambie</option>
<option value=\"ZW\">Zimbabwe</option>
</SELECT>";
}
//création du lien de modification/suppression des modules d'administration
function mklink_CMD($link,$C,$M,$D,$ctxt,$mtxt,$dtxt)
{
if ($D && $M) {$return="<a href=\"".$link."action=M\">$mtxt</a><br><a href=\"".$link."action=D\">$dtxt</a>";}
elseif ($M) {$return="<a href=\"".$link."action=M\">$mtxt</a> ";}
elseif ($D) {$return="<a href=\"".$link."action=D\">$dtxt</a> ";}
elseif ($C){$return="<a href=\"".$link."action=C\">$ctxt</a> ";}
elseif (!$C && !$M && !$D) {$return="<img src=\"module_common_admin/images/0.gif\">";}
return $return;
}
function select_droits ($selected, $id , $auth, $user) //($ligne->c, $ligne->id , "C", 0)
{
print "
<SELECT name=\"".$auth."M[".$id."]\">
<option "; if (!$selected) {print "selected ";} print"value=\"0\">Non</option>
<option "; if ($selected==1) {print "selected ";} print"value=\"1\">Oui</option>";
if ($user)
	{
	print "
	<option "; if ($selected==2) {print "selected ";} print"value=\"2\">User</option>";
	}
print "</select>";
}

function format_datetime ($datetime, $version)
{
return substr($datetime,8,2).".".substr($datetime,5,2).".".substr($datetime,0,4)." � ".substr($datetime,11,5);
}

function format_datetime2 ($datetime, $format)
{
$time=mktime(substr($datetime,11,2),substr($datetime,14,2),substr($datetime,17,2),substr($datetime,5,2),substr($datetime,8,2),substr($datetime,0,4));
$date=date($format,$time );

$date=str_replace("Mon","Lun",$date);
$date=str_replace("Tue","Mar",$date);
$date=str_replace("Wed","Mer",$date);
$date=str_replace("Thu","Jeu",$date);
$date=str_replace("Fri","Ven",$date);
$date=str_replace("Sat","Sam",$date);
$date=str_replace("Sun","Dim",$date);


$date=str_replace("Monday","Lundi",$date);
$date=str_replace("Tuesday","Mardi",$date);
$date=str_replace("Wednesday","Mercredi",$date);
$date=str_replace("Thuesday","Jeudi",$date);
$date=str_replace("Friday","Vendredi",$date);
$date=str_replace("Saterday","Samedi",$date);
$date=str_replace("Sunday","Dimanche",$date);

return $date;
}


function format_date ($date, $format)
{
$date=mktime(substr($date,11,2),substr($date,14,2),substr($date,17,2),substr($date,5,2),substr($date,8,2),substr($date,0,4));


if ($format)
	{
	$dategeneree=date($format,$date);
	}
else
	{
	$dategeneree=date('d.m.Y',$date);
	}
$dategeneree=ereg_replace ("Mon", "Lundi", $dategeneree);
$dategeneree=ereg_replace ("Tue", "Mardi", $dategeneree);
$dategeneree=ereg_replace ("Wed", "Mercredi", $dategeneree);
$dategeneree=ereg_replace ("Thu", "Jeudi", $dategeneree);
$dategeneree=ereg_replace ("Fri", "Vendredi", $dategeneree);
$dategeneree=ereg_replace ("Sat", "Samedi", $dategeneree);
$dategeneree=ereg_replace ("Sun", "Dimanche", $dategeneree);
return $dategeneree;
}

function ical($date)
{
print '<table border="0"><tr><td>';
print '<img border=0 src="../images/cal/month/'.substr($date,5,2).'.gif"><br>';
print '<img border=0 src="../images/cal/day/'.substr($date,8,2).'.gif">';
print '</td></tr></table>';
}


function mk_list_table_class($name,$selected,$sql,$value_field,$text_field, $db, $vide, $class)
{
//entete
print "<select name=\"$name\" class=\"$class\">";
if ($vide) {print "<option value=\"\">---</option>";}
// requete
$res=mysql_query($sql,$db);
while($ligne = mysql_fetch_array ($res))
	{ 
	print "<option value=\"$ligne[$value_field]\" ";
	if ($ligne[$value_field]==$selected)
		{
		print " selected";
		}
	print">$ligne[$text_field]</option>";
	}
//fin
print "</select>";
}
function mk_list_table($name,$selected,$sql,$value_field,$text_field, $db, $vide)
{
//entete
print "<select name=\"$name\">";
if ($vide) {print "<option value=\"\">---</option>";}
// requete
$res=mysql_query($sql,$db);
while($ligne = mysql_fetch_array ($res))
	{ 
	print "<option value=\"$ligne[$value_field]\" ";
	if ($ligne[$value_field]==$selected)
		{
		print " selected";
		}
	print">$ligne[$text_field]</option>";
	}
//fin
print "</select>";
}

function mk_list_table_autosubmit($name,$selected,$sql,$value_field,$text_field, $db, $vide)
{
//entete
print '<select name="'.$name.'" onchange=\'this.form.submit();\'>';
if ($vide) {print "<option value=\"\">---</option>";}
// requete
$res=mysql_query($sql,$db);
while($ligne = mysql_fetch_array ($res))
	{ 
	print "<option value=\"$ligne[$value_field]\" ";
	if ($ligne[$value_field]==$selected)
		{
		print " selected";
		}
	print">$ligne[$text_field]</option>";
	}
//fin
print "</select>";
}

function mk_list_table_autosubmit_multiple($name,$selected,$sql,$value_field,$text_field, $db, $vide, $nombre)
{
//entete
print '<select style="font-size:9px" size="'.$nombre.'" multiple="MULTIPLE" name="'.$name.'" onchange=\'this.form.submit();\'>';
if ($vide) {print "<option value=\"\">---</option>";}
// requete
$res=mysql_query($sql,$db);
while($ligne = mysql_fetch_array ($res))
	{ 
	print "<option value=\"$ligne[$value_field]\" ";
	
		if (is_array($selected) && in_array($ligne[$value_field],$selected))
			{
			print " selected";
			}
	
	print">$ligne[$text_field]</option>";
	}
//fin
print "</select>";
}

function test_login($user,$password)
	{
	$sql="SELECT * FROM admin WHERE user='$user' LIMIT 0,1";
	$res=mysql_query($sql);
	$ligne = mysql_fetch_assoc ($res);
	if ($ligne['password'] && $password && $ligne['password']==$password && $ligne['niveau']>=$level)
		{
		$_SESSION['logged']=TRUE;
		$_SESSION['login_message']="vous êtes logés en tant que ".$ligne['user'];
		$_SESSION['user_id']=$ligne['id'];
		$_SESSION['user']=$ligne['user'];
		$_SESSION['user_nom']=$ligne['nom'];
		$_SESSION['user_prenom']=$ligne['prenom'];
		update_rights ($ligne['id']);
	
		}
	else
		{
		$_SESSION['logged']=FALSE;
		$_SESSION['login_message']="mot de passe incorrect, user inexistant, droit insuffisants ou compte désactivé";
		}
	}
function update_rights ($user_id)
	{
		$sql="SELECT * FROM admin_rights_agregate WHERE id='".$user_id."'";
		$res=mysql_query($sql);
		unset ($_SESSION['rights']);
		while($ligne = mysql_fetch_assoc ($res))
			{
			$_SESSION['rights'][$ligne['module']]['select'] += $ligne['select'];
			$_SESSION['rights'][$ligne['module']]['insert'] += $ligne['insert'];
			$_SESSION['rights'][$ligne['module']]['delete'] += $ligne['delete'];
			$_SESSION['rights'][$ligne['module']]['update'] += $ligne['update'];
			}
	}
function libelle_color($libelle)
	{
	switch ($libelle)
		{
		case Caution: 	return "CCEEFF";
						break;
		case Payement: 	return "CCCCCC";
						break;
		case Location:  return "CCCCCC";
						break;
		case Prestation:return "CCCCCC";
						break;
		case Accompte:	return "CCCCCC";
						break;
		case Achat:		return "CCCCCC";
						break;
		case Frais:		return "CCCCCC";
						break;
		default:		return "FFFFFF";
						break;
		}
	}
function age($datetime, $reference)
	{
	if($reference=="now") {$reference=date("Y-m-d H:i:s");}

	//print "$reference - $datetime<br>";
	$reference=mktime(substr($reference,11,2),substr($reference,14,2),substr($reference,17,2),substr($reference,5,2),substr($reference,8,2),substr($reference,0,4));
	$datetime=mktime(substr($datetime,11,2),substr($datetime,14,2),substr($datetime,17,2),substr($datetime,5,2),substr($datetime,8,2),substr($datetime,0,4));
	//print "$reference - $datetime<br>";
	//print date("Y-m-d H:i:s",$reference)." - ".date("Y-m-d H:i:s",$datetime)."<br>";
	return $reference-$datetime;
	
	}
function temps_cool ($time)
	{
	if ($time<60)
		{
		return $time." sec";
		}
	elseif ($time<3600)
		{
		$min=floor($time/60);
		$sec=$time-($min*60);
		return $min." min ".$sec." sec";
		}
	elseif ($time<86400)
		{
		$hrs=floor($time/3600);
		$min=floor(($time-($hrs*3600))/60);
		$sec=$time-($min*60)-($hrs*3600);
		return $hrs." hrs ".$min." min ".$sec." sec";
		}
	else
		{
		$day=floor($time/86400);
		$hrs=floor(($time-($day*86400))/3600);
		$min=floor(($time-($hrs*3600)-($day*86400))/60);
		$sec=$time-($min*60)-($hrs*3600)-($day*86400);
		return $day." jou ".$hrs." hrs ".$min." min ".$sec." sec";
		}
	}
function desactive_fiche($id, $db)
	{
	$sql="UPDATE fiches_loc_items SET hidden='1' WHERE id_fiche_loc='$id'";
	//print "$sql";
	mysql_query($sql,$db);
	$sql="UPDATE fiches_loc SET hidden='1' WHERE id='$id'";
	//print "$sql";
	mysql_query($sql,$db);
	}
function reactive_item($id, $db)
	{
	$sql="UPDATE fiches_loc_items SET hidden='0' WHERE id='$id'";
	//print "$sql";
	mysql_query($sql,$db);
	}
function randPass($len)
	{
	 $pw = ''; //intialize to be blank
	 for($i=0;$i<$len;$i++)
	 {
	   switch(rand(1,3))
	   {
		 case 1: $pw.=chr(rand(48,57));  break; //0-9
		 case 2: $pw.=chr(rand(65,90));  break; //A-Z
		 case 3: $pw.=chr(rand(97,122)); break; //a-z
	   }
	 }
	 return $pw;
	}

function protege_code($input)
	{
	$output = str_replace("\n", "\n<br />\n", htmlentities($input)); 
	return $output;
	}
// dave
function protege_code_decode($input)
	{
	$output = str_replace("\n", "\n<br />\n", html_entity_decode($input));
	return $output;
	}
// end dave --
function loading_bar ($status, $imgroot)
	{
	if (!$status) {return "<img src=$imgroot/loadingbar/blc22left.gif><img src=$imgroot/loadingbar/blc22mid.gif width=98 height=22><img src=$imgroot/loadingbar/blc22right.gif>";}
	elseif ($status==100) {return "<img src=$imgroot/loadingbar/blue22left.gif><img src=$imgroot/loadingbar/blue22mid.gif width=98 height=22><img src=$imgroot/loadingbar/blue22right.gif>";}
	else {$done=$status-1; $undone=99-$done; return "<img src=$imgroot/loadingbar/blue22left.gif><img src=$imgroot/loadingbar/blue22mid.gif width=$done height=22><img src=$imgroot/loadingbar/blc22mid.gif width=$undone height=22><img src=$imgroot/loadingbar/blc22right.gif>";}
	}
	function date_reform($date, $format)
{
if (!$format) {$format="d/m/Y H:i";}
$output= date($format,mktime(substr($date,-8,2),substr($date,-5,2),substr($date,-2,2),substr($date,5,2),substr($date,8,2),substr($date,0,4)));
$mois_en = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); 
$mois_fr = array("janvier", "f�vrier", "mars", "avril", "mai", "juin", "juillet", "ao�t", "septembre", "octobre", "novembre", "d�cembre"); 
$output = str_replace ($mois_en, $mois_fr, $output); 

$mois_en = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); 
$mois_fr = array("jan", "f�v", "mar", "avr", "mai", "juin", "juil", "ao�t", "sep", "oct", "nov", "d�c"); 
$output = str_replace ($mois_en, $mois_fr, $output); 

$jour_en = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); 
$jour_fr = array("dimanche", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi"); 
$output = str_replace ($jour_en, $jour_fr, $output);

$jour_en = array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"); 
$jour_fr = array("dim", "lun", "mar", "mer", "jeu", "ven", "sam"); 
$output = str_replace ($jour_en, $jour_fr, $output);

$output = preg_replace("/(\D)1st/", "\${1}1er", $output); //1st qui n'est pas pr�c�d� par un chiffre 
$output = preg_replace("/(\d)(st|th|nd|rd)/", "\${1}", $output); //st, th, nd ou rd qui est pr�c�d� d'un chifre 
return $output;
}

function update_cpts_client($id_client,$db)
	{
		
		$sql_cpt="SELECT no FROM cpt_comptes WHERE id_client='$id_client'";
		$res_cpt=mysql_query($sql_cpt,$db);
		$ligne_cpt=mysql_fetch_assoc($res_cpt);
		
		$sql_jour="SELECT no_compte_debit, somme FROM cpt_journal WHERE no_compte_debit='".$ligne_cpt['no']."' OR no_compte_credit='".$ligne_cpt['no']."'";
		$res_jour=mysql_query($sql_jour,$db);
		$solde=0;
		while ($ligne_jour=mysql_fetch_assoc($res_jour))
			{
			//calcul du solde
				if ($ligne_jour['no_compte_debit']==$ligne_cpt['no'])
					{$solde=$solde+$ligne_jour['somme'];}
				else
					{$solde=$solde-$ligne_jour['somme'];}
			}
		//mise � jour du solde
		$sql_update="UPDATE contacts SET solde_en_notre_faveur='$solde' WHERE id='$id_client'";
		//print"<br>$sql_update<br>";
		mysql_query($sql_update,$db);

	}
function update_cpts_fiche_loc($id_fiche_loc,$db)
	{
		
		
		
		$sql_cpt="SELECT cpt.no FROM cpt_comptes cpt, fiches_loc fl WHERE fl.client=cpt.id_client AND fl.id='$id_fiche_loc'";
		$res_cpt=mysql_query($sql_cpt,$db);
		$ligne_cpt=mysql_fetch_assoc($res_cpt);
		
		$sql_jour="SELECT no_compte_debit, somme FROM cpt_journal WHERE no_compte_debit='".$ligne_cpt['no']."' OR no_compte_credit='".$ligne_cpt['no']."'";
		$res_jour=mysql_query($sql_jour,$db);
		$solde=0;
		while ($ligne_jour=mysql_fetch_assoc($res_jour))
			{
			//calcul du solde
				if ($ligne_jour['no_compte_debit']==$ligne_cpt['no'])
					{$solde=$solde+$ligne_jour['somme'];}
				else
					{$solde=$solde-$ligne_jour['somme'];}
			}
		//mise � jour du solde
		$sql_update="UPDATE fiches_loc SET solde_en_notre_faveur='$solde' WHERE id='$id_fiche_loc'";
		//print"<br>$sql_update<br>";
		mysql_query($sql_update,$db);

	}
	
function update_cpts($db)
	{
	$sql="SELECT * FROM contacts";
	
	$res=mysql_query($sql,$db);
	while($ligne=mysql_fetch_object($res))
		{
		
		$sql_cpt="SELECT * FROM cpt_comptes WHERE id_client='$ligne->id'";
		$res_cpt=mysql_query($sql_cpt,$db);
		$ligne_cpt=mysql_fetch_object($res_cpt);
		
		$sql_jour="SELECT * FROM cpt_journal WHERE no_compte_debit='$ligne_cpt->no' OR no_compte_credit='$ligne_cpt->no' ORDER BY date DESC";
		$res_jour=mysql_query($sql_jour,$db);
		$solde=0;
		while ($ligne_jour=mysql_fetch_object($res_jour))
			{
			//calcul du solde
				if ($ligne_jour->no_compte_debit==$ligne_cpt->no)
					{$solde=$solde+$ligne_jour->somme; $texte="débit";}
				else
					{$solde=$solde-$ligne_jour->somme; $texte="crédit";}
			}
		//mise � jour du solde
		$sql_update="UPDATE contacts SET solde_en_notre_faveur='$solde' WHERE id='$ligne->id'";
		//print"$sql_update<br>";
		mysql_query($sql_update,$db);
	
		}
	}



?>